package com.huawei.appgallery.downloadengine.api;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.net.Uri.Builder;
import android.text.TextUtils;
import com.huawei.appgallery.downloadengine.impl.SplitDownloadThreadInfo;
import com.huawei.appgallery.foundation.storage.db.RecordBean;
import com.huawei.appgallery.jsonkit.api.JsonBean;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import o.bnc;
import o.bnl;
import o.dof;
import o.doo;
import org.json.JSONObject;

public class SplitTask extends RecordBean {
    public static final int DOWNLOAD_FAIL = -1;
    public static final int DOWNLOAD_SUCCESS = 0;
    public static final int DOWNLOAD_WAIT = 1;
    public static final String TABLE_NAME = "SplitTask";
    private static final String TAG = "SplitTask";
    private String apkName_;
    private int currentDiffPolicy_ = 0;
    private int currentPolicy_ = 0;
    private String currentSha256_ = null;
    private long currentSize_ = 0;
    private String currentUrl_ = null;
    private String diffSha256_;
    private long diffSize_ = 0;
    private String diffUrl_;
    private String dispatchUrl = null;
    private String downloadPath_;
    private String downloadUrl = null;
    private String featureName_;
    private int fileType_;
    private String filepath_;
    private boolean isCharging = false;
    private boolean isDirectIpMode = false;
    private volatile boolean isInterrupt = false;
    private d lastDownloadErrInfo = new d();
    private String packageName;
    private int protocol = 1;
    private int result_ = 1;
    private SessionDownloadTask sessionDownloadTask;
    private long sessionId_;
    private String sha256_;
    private long sizeDownloaded_ = 0;
    private long size_ = 0;
    private String sliceCheckData_ = "";
    private final List<DownloadChkInfo> sliceChkList = new ArrayList();
    private List<SplitDownloadThreadInfo> splitDownloadThreadInfoList = new CopyOnWriteArrayList();
    private long splitId_;
    private String target_;
    private String url_;

    public static class SliceCheckListBean extends JsonBean {
        private static String TAG = "SliceCheckListBean";
        List<DownloadChkInfo> checkData_;

        public List<DownloadChkInfo> ˎ() {
            return this.checkData_;
        }

        public void ˊ(List<DownloadChkInfo> list) {
            this.checkData_ = list;
        }

        public static SliceCheckListBean ˋ(String str) {
            if (dof.ᐝ(str)) {
                SliceCheckListBean sliceCheckListBean = new SliceCheckListBean();
                try {
                    sliceCheckListBean.fromJson(new JSONObject(str));
                    return sliceCheckListBean;
                } catch (Throwable e) {
                    bnc.ˎ.ˊ(TAG, "fromJsonStr error : ", e);
                    return null;
                } catch (Throwable e2) {
                    bnc.ˎ.ˊ(TAG, "fromJsonStr error : ", e2);
                    return null;
                } catch (Throwable e22) {
                    bnc.ˎ.ˊ(TAG, "fromJsonStr error : ", e22);
                    return null;
                } catch (Throwable e222) {
                    bnc.ˎ.ˊ(TAG, "fromJsonStr error : ", e222);
                    return null;
                } catch (Throwable e2222) {
                    bnc.ˎ.ˊ(TAG, "fromJsonStr error : ", e2222);
                    return null;
                }
            }
            bnc.ˎ.ˊ(TAG, "not a valid json string");
            return null;
        }
    }

    public static class d {
        public String ˎ;
        public int ˏ;

        public String toString() {
            return this.ˏ + "-" + this.ˎ;
        }
    }

    public String t_() {
        return "SplitTask";
    }

    public List<SplitDownloadThreadInfo> ˎ() {
        return this.splitDownloadThreadInfoList;
    }

    public void ˊ() {
        Context ॱ = bnl.ˏ().ॱ();
        if (ॱ != null && this.currentUrl_ != null) {
            try {
                this.currentUrl_ = ॱ(Uri.parse(this.currentUrl_), "net", String.valueOf(doo.ˊ(((ConnectivityManager) ॱ.getSystemService("connectivity")).getActiveNetworkInfo()))).toString();
            } catch (RuntimeException e) {
                bnc.ˎ.ˊ("SplitTask", "addStatisticsParam exception:" + e.getMessage());
            }
        }
    }

    public void ॱ() {
        if (this.currentUrl_ != null && this.sessionDownloadTask != null) {
            try {
                this.currentUrl_ = ॱ(Uri.parse(this.currentUrl_), "isforeground", this.sessionDownloadTask.ॱˎ() ? "1" : "0").toString();
            } catch (RuntimeException e) {
                bnc.ˎ.ˊ("SplitTask", "addIsForegroundInfo exception:" + e.getMessage());
            }
        }
    }

    private Uri ॱ(Uri uri, String str, String str2) {
        Set<String> queryParameterNames = uri.getQueryParameterNames();
        if (!queryParameterNames.contains(str)) {
            return uri.buildUpon().appendQueryParameter(str, str2).build();
        }
        Builder clearQuery = uri.buildUpon().clearQuery();
        for (String str3 : queryParameterNames) {
            clearQuery.appendQueryParameter(str3, str3.equals(str) ? str2 : uri.getQueryParameter(str3));
        }
        return clearQuery.build();
    }

    public void ˏ(boolean z) {
        this.isCharging = z;
    }

    public boolean ˋ() {
        return this.isCharging;
    }

    public String ᐝ() {
        return this.url_;
    }

    public void ˋ(String str) {
        this.url_ = str;
    }

    public String ॱॱ() {
        return this.sha256_;
    }

    public void ˊ(String str) {
        this.sha256_ = str;
    }

    public long ʼ() {
        return this.size_;
    }

    public void ˋ(long j) {
        this.size_ = j;
    }

    public String ʽ() {
        return this.diffUrl_;
    }

    public void ˎ(String str) {
        this.diffUrl_ = str;
    }

    public long ʻ() {
        return this.diffSize_;
    }

    public void ॱ(long j) {
        this.diffSize_ = j;
    }

    public String ˏॱ() {
        return this.diffSha256_;
    }

    public void ˏ(String str) {
        this.diffSha256_ = str;
    }

    public String ॱˊ() {
        return this.filepath_;
    }

    public void ॱ(String str) {
        this.filepath_ = str;
    }

    public String ˊॱ() {
        return this.apkName_;
    }

    public void ʽ(String str) {
        this.apkName_ = str;
    }

    public String ͺ() {
        return this.downloadUrl;
    }

    public void b_(String str) {
        this.downloadUrl = str;
    }

    public String ʻॱ() {
        return this.sliceCheckData_;
    }

    public void ʼ(String str) {
        this.sliceCheckData_ = str;
    }

    public int ᐝॱ() {
        return this.fileType_;
    }

    public void ˎ(int i) {
        this.fileType_ = i;
    }

    public String ॱᐝ() {
        return this.target_;
    }

    public void ॱॱ(String str) {
        this.target_ = str;
    }

    public String ॱˎ() {
        return this.featureName_;
    }

    public void ᐝ(String str) {
        this.featureName_ = str;
    }

    public String ॱˋ() {
        return this.currentUrl_;
    }

    public void ˏॱ(String str) {
        this.currentUrl_ = str;
    }

    public int ʿ() {
        return this.currentPolicy_;
    }

    public void ˋ(int i) {
        this.currentPolicy_ = i;
    }

    public int ʾ() {
        return this.currentDiffPolicy_;
    }

    public void ॱ(int i) {
        this.currentDiffPolicy_ = i;
    }

    public long ʼॱ() {
        return this.currentSize_;
    }

    public void ˎ(long j) {
        this.currentSize_ = j;
    }

    public String ʽॱ() {
        return this.currentSha256_;
    }

    public void ˋॱ(String str) {
        this.currentSha256_ = str;
    }

    public long ˈ() {
        return this.sizeDownloaded_;
    }

    public void ˊ(long j) {
        this.sizeDownloaded_ = j;
    }

    public int ˊᐝ() {
        return this.result_;
    }

    public void ˏ(int i) {
        this.result_ = i;
    }

    public String ˊˊ() {
        return this.downloadPath_;
    }

    public void ͺ(String str) {
        this.downloadPath_ = str;
    }

    public int ˋˊ() {
        return this.protocol;
    }

    public void ˊ(int i) {
        this.protocol = i;
    }

    public long ˉ() {
        return this.sessionId_;
    }

    public void ˏ(long j) {
        this.sessionId_ = j;
    }

    public long ˊˋ() {
        return this.splitId_;
    }

    public void ᐝ(long j) {
        this.splitId_ = j;
    }

    public String ˎˎ() {
        return this.packageName;
    }

    public void ˊॱ(String str) {
        this.packageName = str;
    }

    public SessionDownloadTask ˋᐝ() {
        return this.sessionDownloadTask;
    }

    public void ˊ(SessionDownloadTask sessionDownloadTask) {
        this.sessionDownloadTask = sessionDownloadTask;
    }

    public List<DownloadChkInfo> ˍ() {
        if (!this.sliceChkList.isEmpty()) {
            return this.sliceChkList;
        }
        if (!TextUtils.isEmpty(this.sliceCheckData_)) {
            SliceCheckListBean ˋ = SliceCheckListBean.ˋ(this.sliceCheckData_);
            if (ˋ != null) {
                List ˎ = ˋ.ˎ();
                if (!(ˎ == null || ˎ.isEmpty())) {
                    int i = 0;
                    while (i < ˎ.size()) {
                        try {
                            String[] split = ((DownloadChkInfo) ˎ.get(i)).ˊ().split("-");
                            if (split.length == 2) {
                                ((DownloadChkInfo) ˎ.get(i)).ˏ(Long.parseLong(split[0]));
                                ((DownloadChkInfo) ˎ.get(i)).ˋ(Long.parseLong(split[1]));
                                ((DownloadChkInfo) ˎ.get(i)).ॱ(0);
                            }
                            i++;
                        } catch (RuntimeException e) {
                            bnc.ˎ.ˏ("SplitTask", "getSliceChkList exception:" + e.toString());
                        }
                    }
                    synchronized (this.sliceChkList) {
                        if (this.sliceChkList.isEmpty()) {
                            this.sliceChkList.addAll(ˋ.ˎ());
                            bnc.ˎ.ˋ("SplitTask", "sliceChkList restored`");
                        }
                    }
                }
            }
        }
        return this.sliceChkList;
    }

    public void ˋ(List<DownloadChkInfo> list) {
        this.sliceChkList.clear();
        this.sliceCheckData_ = "";
        if (list != null && list.size() > 0) {
            int size = list.size();
            for (int i = 0; i < size - 1; i++) {
                if (((DownloadChkInfo) list.get(i)).ˋ() + 1 != ((DownloadChkInfo) list.get(i + 1)).ˎ()) {
                    bnc.ˎ.ˊ("SplitTask", "bad slice data, package=" + (dof.ˋ(ˎˎ()) ? HwAccountConstants.NULL : ˎˎ()));
                    return;
                }
            }
        }
        this.sliceChkList.addAll(list);
        SliceCheckListBean sliceCheckListBean = new SliceCheckListBean();
        sliceCheckListBean.ˊ(this.sliceChkList);
        try {
            ʼ(sliceCheckListBean.toJson());
        } catch (Exception e) {
            bnc.ˎ.ˊ("SplitTask", "toJson exception :" + e.getMessage());
        }
    }
}
